package org.yzz.auth.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yzz.auth.annotation.RonghuaPermission;
import org.yzz.auth.domain.Role;
import org.yzz.auth.dto.MenuDTO;
import org.yzz.auth.dto.RoleDTO;
import org.yzz.auth.query.RoleQuery;
import org.yzz.auth.service.IRoleService;
import org.yzz.base.utils.AjaxResult;
import org.yzz.base.utils.PageInfo;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@RonghuaPermission(name = "角色管理")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @GetMapping
    @RonghuaPermission(name = "查询全部角色")
    @ApiOperation(value = "查询全部角色" )
    public AjaxResult selectAll(){
        try {
            List<Role> roles = roleService.selectAll();
            return AjaxResult.success(roles);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @RonghuaPermission(name = "通过ID查询角色")
    @ApiOperation(value = "通过ID查询角色" )
    public AjaxResult selectById(@PathVariable Long id){
        try {
            Role Role = roleService.selectById(id);
            return AjaxResult.success(Role);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    //通过名字查询
    /*@GetMapping("/{username}")
    public void selectByName(String username){

    }*/

    /**
     * 新增或修改
     * @param Role
     */
    @PostMapping
    @RonghuaPermission(name = "角色新增或修改")
    @ApiOperation(value = "新增或修改" )
    public AjaxResult save(@RequestBody Role Role){
        try {
            if(Objects.nonNull(Role.getId())){
                roleService.update(Role);
                return AjaxResult.success();
            }
            roleService.insert(Role);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "角色根据指定 id 删除")
    @ApiOperation(value = "删除" )
    public AjaxResult delete(@PathVariable Long id){
        try {
            roleService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param roleQuery
     * @return
     */
    @PutMapping
    @RonghuaPermission(name = "角色分页查询")
    public AjaxResult queryPage(@RequestBody RoleQuery roleQuery){
        try {
            PageInfo<Role> pageInfo = roleService.queryPage(roleQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    @RonghuaPermission(name = "角色批量删除")
    public  AjaxResult batchDelete(@RequestBody  List<Long> ids){
        try {
            roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 保存权限
     * @param
     * @return
     */
    @PostMapping("/permission")
    @RonghuaPermission(name = "角色新增或修改")
    @ApiOperation(value = "新增或修改" )
    public AjaxResult insertByRoleId(@RequestBody RoleDTO roleDTO){
        try {
            roleService.insertByRoleId(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 保存 菜单
     * @param
     * @return
     */
    @PostMapping("/menu")
    @RonghuaPermission(name = "角色新增或修改")
    @ApiOperation(value = "新增或修改" )
    public AjaxResult insertByMenuId(@RequestBody MenuDTO menuDTO){
        try {
            roleService.insertByMenuId(menuDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据角色 id 查询对应的权限
     * @param
     * @return
     */
    @GetMapping("/permission/{id}")
    @RonghuaPermission(name = "根据角色 id 查询对应的权限")
    @ApiOperation(value = "根据角色 id 查询对应的权限" )
    public AjaxResult selectByRoleId(@PathVariable Long id){
        try {
            List<String> sns=roleService.selectByRoleId(id);
            return AjaxResult.success(sns);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/ids")
    @RonghuaPermission(name = "查询全部角色id")
    @ApiOperation(value = "查询全部角色id" )
    public AjaxResult selectAllId(){
        try {
            List<Long> roles = roleService.selectAllId();
            return AjaxResult.success(roles);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/menu/{id}")
    @RonghuaPermission(name = "查询角色对应的菜单")
    @ApiOperation(value = "查询角色对应的菜单" )
    public AjaxResult selectMenusById(@PathVariable Long id){
        try {
            List<Long> ids = roleService.selectMenusById(id);
            return AjaxResult.success(ids);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
